package com.shoe.service.impl;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.shoe.mapper.ShoeGoodMapper;
import com.shoe.pojo.ShoeCollectExample;
import com.shoe.pojo.ShoeGood;
import com.shoe.pojo.ShoeGoodExample;
import com.shoe.pojo.ShoeOrder;
import com.shoe.service.ShoeGoodsService;
import com.shoe.vo.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.xml.crypto.Data;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class ShoeGoodsServiceImpl implements ShoeGoodsService {

    @Autowired
    ShoeGoodMapper shoeGoodMapper;


    @Override
    public List<ShoeGood> selectAll() {

        List<ShoeGood> list = shoeGoodMapper.selectByExample(null);

        return list;
    }

    @Override
    public ResponseResult selectAll(int currPage, int pageSize) {
        PageHelper.startPage(currPage, pageSize);
        List<ShoeGood> list = shoeGoodMapper.selectByExample(null);
        PageInfo<ShoeGood> pageInfo = new PageInfo<>(list);
        // 设置导航页数
        pageInfo.setNavigatePages(10);
        ResponseResult responseResult = new ResponseResult();
        responseResult.setData(pageInfo);
        return responseResult;
    }

    @Override
    public ShoeGood selectOne(Long goodId) {
        ShoeGood shoeGood = shoeGoodMapper.selectByPrimaryKey(goodId);
        return shoeGood;
    }

    @Override
    public ResponseResult selectOther(int currPage, int pageSize, ShoeGood shoeGood) {
        System.out.println(shoeGood.getGoodSex());
        PageHelper.startPage(currPage, pageSize);
        List<ShoeGood> list = shoeGoodMapper.selectAllAnyShoeGood(shoeGood);


        System.out.println(list);

        PageInfo<ShoeGood> pageInfo = new PageInfo<>(list);
        // 设置导航页数
        pageInfo.setNavigatePages(10);
        ResponseResult responseResult = new ResponseResult();
        responseResult.setData(pageInfo);
        return responseResult;
    }

    @Override
    public ShoeGood selectById(Long id) {
       ShoeGood shoeGood = shoeGoodMapper.selectByPrimaryKey(id);

        return shoeGood;
    }

    @Override
    public int addGoods(ShoeGood shoeGood) {
      int a =  shoeGoodMapper.insert(shoeGood);

        return a;
    }

    @Override
    public List<ShoeGood> selectPage(Integer page, Integer limit) {
        Map<String,Integer> map = new HashMap<String, Integer>();
        map.put("page",page);
        map.put("limit",limit);
        List<ShoeGood> list = shoeGoodMapper.selectPage(map);
        return list;
    }

    @Override
    public List<ShoeGood> selectPart(ShoeGood shoeGood, String age) {
        ShoeGoodExample shoeGoodExample = new ShoeGoodExample();
        Date date = new Date();
        SimpleDateFormat dft = new SimpleDateFormat("yyyy-MM-dd");
        Date beginDate = new Date();
        Calendar date1 = Calendar.getInstance();
        date1.setTime(beginDate);
        date1.set(Calendar.DATE, date1.get(Calendar.DATE) - Integer.parseInt(age));
        try {
            Date endDate = dft.parse(dft.format(date1.getTime()));

            if(shoeGood.getGoodName() != null){
                shoeGoodExample.createCriteria().andGoodBrandEqualTo(shoeGood.getGoodBrand()).andGoodDateBetween(endDate,date).andGoodNameLike("%" +shoeGood.getGoodName() + "%");
            }else {
                shoeGoodExample.createCriteria().andGoodBrandEqualTo(shoeGood.getGoodBrand()).andGoodDateBetween(endDate,date);
            }

          List<ShoeGood> list =  shoeGoodMapper.selectByExample(shoeGoodExample);
          return list;
        } catch (ParseException e) {
            e.printStackTrace();
        }

        return null;
    }

    @Override
    public int updatepart(Long id) {
        ShoeGoodExample shoeGoodExample = new ShoeGoodExample();
        ShoeGood shoeGood = new ShoeGood();
        int a = 0;

            shoeGood.setGoodId(id);
            shoeGood.setReserve1("已下架");
            a = shoeGoodMapper.updateByPrimaryKeySelective(shoeGood);

        return a;
    }


}
